package com.heima.Listener;

import com.alibaba.fastjson.JSON;
import com.heima.Serivce.WemediaSensitiveSerivce;
import com.heima.model.Admin.Dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import static com.heima.common.exception.RabbitMQcode.*;

@Slf4j
@Component
public class SensitiveListener {
    @Autowired
    WemediaSensitiveSerivce wemediaSensitiveSerivce;

    //    敏感词相关操作
    @RabbitListener(queues = WmSensitiveQueueName)
    public void Sensit6iveoperate(String data) {
        log.info("接收消息成功");
        String tpye = data.split("_")[1];
        String Object = data.split("_")[0];
        if (tpye.equals("del"))
        {
            SensitiveDel(Object);
            log.info("Sensitive删除成功");
        }

        if (tpye.equals("save"))
        {
            SaveSensitive(Object);
            log.info("Sensitive添加成功");
        }

        if(tpye.equals("update"))
        {
            UpdateSensitive(Object);
            log.info("Sensitive修改成功");
        }
    }

    private Boolean SensitiveDel(String data) {
        Integer id=JSON.parseObject(data,Integer.class);
        wemediaSensitiveSerivce.DeleteSensitive(id);

        return true;
    }

    private Boolean SaveSensitive(String data)
    {
        WmSensitive wmSensitive=JSON.parseObject(data,WmSensitive.class);
        wemediaSensitiveSerivce.SaveSensitive(wmSensitive);
        return true;
    }

    private Boolean UpdateSensitive(String data)
    {
        WmSensitive wmSensitive=JSON.parseObject(data,WmSensitive.class);
        wemediaSensitiveSerivce.UpdateSensitive(wmSensitive);
        return true;
    }


}
